const STORAGE_KEY = 'mechat_data';

export const getCachedData = (key) => {
  try {
    const data = localStorage.getItem(key);
    if (!data) return null;
    return data;
  } catch (error) {
    console.error('读取缓存数据失败:', error);
    return null;
  }
};

export const setCachedData = (key, value) => {
  try {
    const existingData = localStorage.getItem(STORAGE_KEY) || '{}';
    const parsedData = JSON.parse(existingData);
    
    const newData = {
      ...parsedData,
      [key]: value
    };
    
    localStorage.setItem(STORAGE_KEY, JSON.stringify(newData));
    return true;
  } catch (error) {
    console.error('保存缓存数据失败:', error);
    return false;
  }
};

export const clearCachedData = () => {
  localStorage.removeItem(STORAGE_KEY);
};